﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace WinAPI.User32
{
    public class OperateControls
    {
        private IntPtr _targetWindow;

        public IntPtr TargetWindow
        {
            get { return _targetWindow; }
        }


        public bool Report(IntPtr hwnd, string lParam)
        {
            StringBuilder sb = new StringBuilder(100);
            User32Dll.GetWindowText(hwnd, sb, 100);
            if (sb.ToString().StartsWith(lParam))
            {
                _targetWindow = hwnd;
                return false;
            }
            return true;
        }

        public void OperateButton()
        {

            IntPtr ptrNextBtn = User32Dll.FindWindowEx(_targetWindow, IntPtr.Zero, null, "button4");

            //User32Dll.SetForegroundWindow(_targetWindow);                      //将combobox的父窗口设为当前活动窗口
            //User32Dll.SendMessage(ptrNextBtn, User32Dll.WM_LBUTTONDOWN, new IntPtr(0), new IntPtr(0));//打开列表
            //User32Dll.SendMessage(ptrNextBtn, User32Dll.WM_LBUTTONUP, new IntPtr(0), new IntPtr(0));//打开列表
            StringBuilder stringBuilder = new StringBuilder(100);
            int id = User32Dll.GetDlgCtrlID(ptrNextBtn);
            User32Dll.GetDlgItemText(_targetWindow, id, stringBuilder, 100);
            User32Dll.SendDlgItemMessage(_targetWindow, id, 0x00F5, IntPtr.Zero, IntPtr.Zero);

            //BM_CLICK
        }


        public void OperateDropDown()
        {

            IntPtr ptrNextBtn = User32Dll.FindWindowEx(_targetWindow, IntPtr.Zero, "Sandbox:DefaultBox:WindowsForms10.COMBOBOX.app.0.378734a", "");                                                //找到combobox的句柄 

            User32Dll.SetForegroundWindow(_targetWindow);                      //将combobox的父窗口设为当前活动窗口
            User32Dll.SendMessage(ptrNextBtn, User32Dll.CB_SHOWDROPDOWN, new IntPtr(1), new IntPtr(0));//打开列表  

            const string ptr = "1";
            User32Dll.SendMessage(ptrNextBtn, User32Dll.CB_SELECTSTRING, IntPtr.Zero, Marshal.StringToHGlobalAnsi(ptr));//选择 
            User32Dll.SendMessage(ptrNextBtn, User32Dll.WM_KEYDOWN, (IntPtr)Vks.VK_RETURN, IntPtr.Zero);  //响应回车键 
            User32Dll.SendMessage(ptrNextBtn, User32Dll.CB_SHOWDROPDOWN, IntPtr.Zero, IntPtr.Zero); //关闭列表   
            StringBuilder stringBuilder = new StringBuilder(100);
            User32Dll.SendMessage(ptrNextBtn, User32Dll.WM_GETTEXT, new IntPtr(100), stringBuilder);
        }

        public bool GotoXyAndDbClick(float x, float y)
        {
            if (_targetWindow != IntPtr.Zero)
            {
                RECT rectClient = new RECT();
                User32Dll.GetClientRect(_targetWindow, ref rectClient);

                User32Dll.SendMessage(_targetWindow, User32Dll.WM_MOUSEMOVE, IntPtr.Zero, User32Dll.MakeLParam(0, 0));
                StringBuilder tilteUpperLeft = new StringBuilder(100);
                User32Dll.GetWindowText(_targetWindow, tilteUpperLeft, 100);

                User32Dll.SendMessage(_targetWindow, User32Dll.WM_MOUSEMOVE, IntPtr.Zero, User32Dll.MakeLParam(rectClient.right, rectClient.bottom));
                StringBuilder titleBottomRight = new StringBuilder(100);
                User32Dll.GetWindowText(_targetWindow, titleBottomRight, 100);

                tilteUpperLeft.Replace("{", string.Empty);
                tilteUpperLeft.Replace("}", string.Empty);
                titleBottomRight.Replace("{", string.Empty);
                titleBottomRight.Replace("}", string.Empty);

                string[] tilteUpperLeftSplit = tilteUpperLeft.ToString().Split(new string[] { "," },
                                                                               StringSplitOptions.RemoveEmptyEntries);
                string[] titleBottomRightSplit = titleBottomRight.ToString().Split(new string[] { "," },
                                                                              StringSplitOptions.RemoveEmptyEntries);

                int[] hor = new int[] { Convert.ToInt32(tilteUpperLeftSplit[0].Substring(2)), Convert.ToInt32(tilteUpperLeftSplit[1].Substring(2)) };
                int[] ver = new int[] { Convert.ToInt32(titleBottomRightSplit[0].Substring(2)), Convert.ToInt32(titleBottomRightSplit[1].Substring(2)) };

                int xt = (int)((x - hor[0]) / (ver[0] - hor[0]) * rectClient.right);
                int yt = (int)((y - hor[1]) / (ver[1] - hor[1]) * rectClient.bottom);

                IntPtr pos = User32Dll.MakeLParam(xt, yt);

                User32Dll.SendMessage(_targetWindow, User32Dll.WM_MOUSEMOVE, IntPtr.Zero, pos);

                User32Dll.SendMessage(_targetWindow, User32Dll.WM_LBUTTONDOWN, IntPtr.Zero, pos);
                Thread.Sleep(10);
                User32Dll.SendMessage(_targetWindow, User32Dll.WM_LBUTTONUP, IntPtr.Zero, pos);
                Thread.Sleep(10);
                User32Dll.SendMessage(_targetWindow, User32Dll.WM_LBUTTONDOWN, IntPtr.Zero, pos);
                Thread.Sleep(10);
                User32Dll.SendMessage(_targetWindow, User32Dll.WM_LBUTTONUP, IntPtr.Zero, pos);

                int result = User32Dll.SendMessage(_targetWindow, User32Dll.WM_LBUTTONDBLCLK, IntPtr.Zero, pos);

                return result != 0;
            }
            return false;
        }
    }
}
